package com.hzf.wxplatform.web.controller.manager.material;

import com.hzf.api.rpc.api.ApiService;
import com.hzf.common.base.BaseController;
import com.hzf.common.util.ByteUtils;
import com.hzf.material.dao.model.*;
import com.hzf.order.dao.model.*;
import com.hzf.ucenter.dao.model.UcenterUser;
import com.hzf.wxplatform.common.WxplatformResult;
import com.hzf.wxplatform.common.WxplatformResultConstant;
import com.hzf.wxplatform.common.utils.UcenterUserUtils;
import com.sun.corba.se.impl.ior.NewObjectKeyTemplateBase;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * describe: TODO
 * creat_user: 白开水
 * creat_time: 2017/12/13   17:04
 **/
@Controller
@RequestMapping("/materialOrder")
public class MaterialOrderController extends BaseController {
    @Autowired
    ApiService apiService;
    @RequestMapping(value = "/index")
        public String index(Model model, HttpServletRequest request){
         byte[] user = UcenterUserUtils.getUser(request.getSession());
        System.out.println(user);
         UcenterUser   ucenterUser = (UcenterUser) ByteUtils.ByteToObject(user);
         if(null==ucenterUser){
            model.addAttribute("data","您的操作属于非法操作，所查看商品或设计师非您账户，请核实。");
            return  thymeleaf("/404");
        }
         MaterialShopExample materialShopExample = new MaterialShopExample();
         materialShopExample.createCriteria().andUserIdEqualTo(ucenterUser.getId());
         MaterialShop materialShop = apiService.selectByShop(materialShopExample);
         List<OrderUserInfo>list =apiService.seleclUserOrderInfoList(materialShop.getShopId());
         model.addAttribute("list",list);
         return  thymeleaf("/CLS/CLS_DD_Ticket");
        }
    @RequestMapping(value="/order")
    public String order(Model model,HttpServletRequest servletRequest,int id){
        byte[] user = UcenterUserUtils.getUser(servletRequest.getSession());
        UcenterUser   ucenterUser = (UcenterUser) ByteUtils.ByteToObject(user); if(null==ucenterUser){
            model.addAttribute("data","您的操作属于非法操作，所查看商品或设计师非您账户，请核实。");
            return  thymeleaf("/404");
        }
        MaterialShopExample materialShopExample = new MaterialShopExample();
        materialShopExample.createCriteria().andUserIdEqualTo(ucenterUser.getId());
        MaterialShop materialShop = apiService.selectByShop(materialShopExample);
        OrderUserInfoExample example = new OrderUserInfoExample();
        example.createCriteria().andShopIdEqualTo(materialShop.getShopId()).andOrderUserInfoIdEqualTo(id);
        List<OrderUserInfo>list =apiService.selectOrderInfoList(example);
        if(0==list.size()){
            model.addAttribute("data","您的操作属于非法操作，所查看商品或设计师非您账户，请核实。");
            return  thymeleaf("/404");
        }else{
            OrderUserInfo orderUserInfo = new OrderUserInfo();
            orderUserInfo = list.get(0);
            MaterialGoodsExample materialGoods = new MaterialGoodsExample();
            materialGoods.createCriteria().andGoodsIdEqualTo(orderUserInfo.getGoodsId());
            MaterialGoodsModelExample materialGoodsModel = new MaterialGoodsModelExample();
            materialGoodsModel.createCriteria().andGoodsIdEqualTo(orderUserInfo.getGoodsId()).andModelIdEqualTo(orderUserInfo.getModelId());
            orderUserInfo.setMaterialGoods(apiService.selectByGoods(materialGoods));
            List<MaterialGoodsModel> modelList = apiService.selectByMaterialGoodsModel(materialGoodsModel);
            orderUserInfo.setMaterialGoodsModel(modelList.get(0));
            OrderUserExample orderUserExample = new OrderUserExample();
            orderUserExample.createCriteria().andIdEqualTo(orderUserInfo.getOrderUserId()).andOrderStateEqualTo(0);
            OrderUser orderUser = apiService.selectOrder(orderUserExample);
            model.addAttribute("orderUser",orderUser);
            model.addAttribute("orderUserInfo",orderUserInfo);
            return  thymeleaf("/CLS/CLS_DD_DingDXQ");

        }

    }
    @RequestMapping(value="/fahuo",method = RequestMethod.POST)
    @ResponseBody
    public Object fahuo(Model model,HttpServletRequest request,int id,int type,long phone){
        byte[] user = UcenterUserUtils.getUser(request.getSession());
        UcenterUser   ucenterUser = (UcenterUser) ByteUtils.ByteToObject(user);
        if(null==ucenterUser){
            return new WxplatformResult(WxplatformResultConstant.FAILED, "您的操作属于非法操作，请登录后操作。");

        }
        MaterialShopExample materialShopExample = new MaterialShopExample();
        materialShopExample.createCriteria().andUserIdEqualTo(ucenterUser.getId());
        MaterialShop materialShop = apiService.selectByShop(materialShopExample);
        OrderUserInfo orderUserInfo = new OrderUserInfo();
        OrderUserInfoExample example = new OrderUserInfoExample();
        example.createCriteria().andShopIdEqualTo(materialShop.getShopId()).andOrderUserInfoIdEqualTo(id);
        List<OrderUserInfo>list =apiService.selectOrderInfoList(example);
        if(0==list.size()){
            return new WxplatformResult(WxplatformResultConstant.FAILED, "您的操作属于非法操作，所查看商品或设计师非您账户，请核实。");
        }
            if(type==1){
                orderUserInfo = list.get(0);
                orderUserInfo.setPayState(2);
                apiService.updateOrderInfo(orderUserInfo);
                OrderNotity orderNotity = new OrderNotity();
                orderNotity.setNotityState(0);
                orderNotity.setUserId(ucenterUser.getId());
                orderNotity.setPeisongPhone(phone);
                orderNotity.setOrderUserInfoId(orderUserInfo.getOrderUserInfoId());
                apiService.installOrderNotity(orderNotity);
                return new WxplatformResult(WxplatformResultConstant.SUCCESS, "确认发货");
            }else if(type==0){
                orderUserInfo = list.get(0);
                orderUserInfo.setPayState(2);
                apiService.updateOrderInfo(orderUserInfo);
                return new WxplatformResult(WxplatformResultConstant.SUCCESS, "已成功修改订单状态：已发货");
            }

     return null;
    }
}
